//演练室
var getclassrooms = {
		getclassrooms_list : function() {
		if(sessionStorage.getItem("suid")==null){
				return;
			}
		var suid = JSON.parse(sessionStorage.getItem("suid")).data.su_id;
		$.ajax({
					type : "GET",
					url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=getclassrooms,suid="+ suid + ",alive=1&address=http://103.37.166.4:7777/StateGrid/cra.do",
//					async : false,
					scriptCharset : 'utf-8',
					success : function(param) {
						param = JSON.parse(param).data;
						getclassrooms.createTable(param);
//						alert(param);
					},
					error : function() {
					}
				});
	},createTable : function(params){
		var str = "";
		var j = 0;
		for ( var i in params) {
			j ++;
			if(params[i].alive=="1"){
				params[i].alive = "加入";
			}else if(params[i].alive=="0"){
				params[i].alive = "关闭";
			}
//				function displayHideUI()
//				{
//				var ui =document.getElementById("bbs");
//				ui.style.display="none";
//				}
			if(params[i].isDel!="1"){
				var param = JSON.stringify(params[i]).replace(new RegExp('"','gm'),'|');
				if(i==0){
					str += '<tr class="active" id="tr'+j+'" onclick="createContent(\''+param+'\',\'tr'+j+'\')")"><td class="right">'+j+'</td><td colspan="8"><span class="havaBg"></span>'+params[i].ecdClassRoomName+'</td></tr>';
					getclassrooms.selectedRoom = params[i].ecdClassRoomId;
					getclassrooms.selectedContent = params[i];
					createContent(param,'tr'+j);
				}else{
					str += '<tr id="tr'+j+'" onclick="createContent(\''+param+'\',\'tr'+j+'\')")"><td class="right">'+j+'</td><td colspan="8"><span class="havaBg"></span>'+params[i].ecdClassRoomName+'</td></tr>';
				}
			}
		}
		$(str).appendTo("#getclassrooms_list")
	},
	selectedId : "tr1",
	selectedRoom : "",
	selectedContent: {}
	
}

function replaceAll(param){
	while(param.indexOf("|") > -1){
		param = param.replace('|','"');
	}
	return param;
}

function  createContent(param,id){
	$("#"+getclassrooms.selectedId).removeClass('active');
	$("#"+id).addClass('active');
	getclassrooms.selectedId = id;
	param = replaceAll(param);
	sessionStorage.setItem("param", param);
	param = JSON.parse(param);
	getclassrooms.selectedContent = param;
	getclassrooms.selectedRoom = param.ecdClassRoomId;
	sessionStorage.setItem("crid",param.ecdClassRoomId);
	
	//获取演练室的信息
	sessionStorage.setItem("ecdClassRoomName", param.ecdClassRoomName) 
//	$("#ecdClassRoomName").html(param.ecdClassRoomName);
//	$("#projectName").val(param.projectName);
//	$("#ecdClassRoomDescription").html(param.ecdClassRoomDescription);
}

//立即加载
$(function() {
	getclassrooms.getclassrooms_list();
	
	//刷新
	$("#renovate").click(function(){
		location.reload();
		
	})
	
	
	
	$("#strat_add").click(function(){
		var	params = sessionStorage.getItem("param");
		var crid=getclassrooms.selectedRoom;
		var suid = JSON.parse(sessionStorage.getItem("suid")).data.su_id;
		$.ajax({
			type : "GET",
			url : "http://127.0.0.1:8080/yl-websocket/transmit?param='m=joinclassroom,suid="+suid+",crid="+ crid +"&address=http://103.37.166.4:7777/StateGrid/cra.do",
//			async: false,
			scriptCharset: 'utf-8', 
			success : function(param) {
				//后台返回状态
//				alert(param);
				if(JSON.parse(param).stat == "1"){
					sessionStorage.setItem("crid", crid);
					location.href='studentBegin.html';
				}else{
					
				}
				
			},
			error: function(){
			}
		});
	});
})



